package com.hxd.mall.utils.open

import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.hxd.mall.R
import com.hxd.mall.widget.GridItemDecoration
import com.hxd.mall.widget.HxdItemDecorationView
import com.hxd.mall.widget.HxdStateFrameView

/**
 * Create by yinzhengwei on 2019/4/9
 * @Function
 */
fun RecyclerView.initRecyclerView(
    baseAdapter: BaseQuickAdapter<*, *>, orientation: Int = RecyclerView.VERTICAL,
    decorationOrientation: Int = LinearLayoutCompat.VERTICAL,
    padding: Int = 1, leftPadding: Int = 20, color: Int = R.color.transparent
) {
    adapter = baseAdapter
    setHasFixedSize(true)
    layoutManager = LinearLayoutManager(context, orientation, false)

    initRecyclerView()
    HxdItemDecorationView(
        decorationOrientation,
        UIUtil.dip2px(padding),
        ContextCompat.getColor(context, color),
        UIUtil.dip2px(leftPadding)
    ).run {
        addItemDecoration(this)
    }
}

fun RecyclerView.initRecyclerView(decorationOrientation: Int = LinearLayoutCompat.VERTICAL, color: Int = R.color.transparent) {
    HxdItemDecorationView(
        decorationOrientation,
        UIUtil.dip2px(0),
        ContextCompat.getColor(context, color),
        UIUtil.dip2px(0)
    ).run {
        addItemDecoration(this)
    }
}

fun RecyclerView.initGridRecyclerView(
    baseAdapter: BaseQuickAdapter<*, *>,
    rowSpace: Int = 0,
    columnSpace: Int = 0,
    spanCount: Int = 2
) {
    Log.d("initGridRecyclerView", "initGridRecyclerView")
    adapter = baseAdapter
    setHasFixedSize(true)
    layoutManager = GridLayoutManager(context, spanCount, LinearLayoutManager.VERTICAL, false)
    GridItemDecoration(UIUtil.dip2px(rowSpace), UIUtil.dip2px(columnSpace), spanCount).run {
        addItemDecoration(this)
    }
}

fun RecyclerView.addRecyclerMargin() {
    //add view margin
    LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    ).run {
        topMargin = UIUtil.dip2px(12)
        layoutParams = this
    }
}

fun HxdStateFrameView.bindSuccessfulView() {
    View.inflate(context, R.layout.activity_list_repay, null).run {
        bindSuccessView(this)
    }
}

fun HxdStateFrameView.showEmptyView(defaultText: String) {
    showEmptyView()
    emptyView?.findViewById<TextView>(R.id.widget_tv_empty_data)?.run {
        text = defaultText
    }
}

fun HxdStateFrameView.showErrorView(callback: () -> Unit) {
    showErrorView()
    setOnReloadListener(object : HxdStateFrameView.OnReloadListener {
        override fun onReload() {
            callback()
        }
    })
}

fun HxdStateFrameView.netWorkUnUse(): Boolean {
    if (NetUtil.getNetworkType(context) == -1) {
        showNoNetwork()
        return true
    }
    return false
}